import {environment} from '../../../../environments/environment';
import {Token} from "../commons/base.data";
import {globalCache} from "../commons/cache.manage";

export enum UrlType {
    PAGE, DATA
}

export class Url {

    public static readonly enums: Url[] = [];

    /* 登陆地址 */
    public static readonly LOGIN: Url = new Url('index', UrlType.PAGE);
    public static readonly LOGIN_SYSTEM: Url = new Url('system/info', UrlType.DATA);
    public static readonly LOGIN_COPYRIGHT: Url = new Url('system/login/copyright', UrlType.DATA);
    public static readonly LOGIN_DATA: Url = new Url('oauth/token', UrlType.DATA);
    public static readonly LOGIN_REGISTER_DATA: Url = new Url('system/register/data', UrlType.DATA);
    public static readonly LOGIN_SEND_CODE: Url = new Url('system/captcha/send/data', UrlType.DATA);

    public static readonly HOME_VERSION_DATA: Url = new Url('home/version', UrlType.DATA);

    /* 注销地址 */
    public static readonly LOGOUT: Url = new Url('system/logout', UrlType.DATA);

    /* 主页地址 */
    public static readonly WORKBENCH: Url = new Url('workbench', UrlType.PAGE);

    constructor(private _url: string, private _type: UrlType) {
        Url.enums.push(this);
    }

    /**
     * 获取当前类型所有的枚举类实例只读数组的函数
     * @return {ReadonlyArray<Url>} 返回当前类型所有的枚举类实例只读数组
     */
    public static values(): ReadonlyArray<Url> {
        return Object.freeze(Url.enums);
    }

    /**
     * 根据枚举名称获取统一资源定位符对象的函数
     * @param url URL地址
     * @return {Url} 返回统一资源定位符对象（如果ID无效则返回null）
     */
    public static urlOf(url: string): Url {
        const values: ReadonlyArray<Url> = Url.values();
        for (const obj of values) {
            if (obj.url === url) {
                return obj;
            }
        }
        throw new Error(`您访问的页面地址无效，url = ${url}`);
    }

    public static jumpVerify(result: boolean) {
        if (!result) {
            throw new Error(`请求页面失败，请求地址：${Url.LOGIN.url}`);
        }
    }

    get url(): string {
        if (environment.production || this.type === UrlType.PAGE) {
            return this._url;
        }
        return `/assets/test/${this._url}.json`;
    }

    get tokenUrl(): string {
        const token: Token = globalCache.getToken();
        if (token && token.access_token) {
            return `${this._url}?access_token=${token.access_token}`;
        }
        throw new Error("当前地址无效，请刷新页面后再试。");
    }

    get type(): UrlType {
        return this._type;
    }

    /**
     * 获取当前枚举类型序号的函数
     * @return {number} 返回当前枚举类型序号
     */
    public ordinal(): number {
        return Url.enums.indexOf(this);
    }

    /**
     * 获取当前枚举类型名称的函数
     * @return {string} 返回当前枚举类型的名称
     */
    public name(): string {
        const keys: string[] = Object.keys(this.constructor);
        keys.splice(0, 2);  // 排除掉enums和_localeKeys
        return keys[this.ordinal()];
    }

    /**
     * 将当前枚举类型转换为字符串的函数
     * @return {string} 返回当前枚举类型的名称
     */
    public toString(): string {
        return this.name();
    }

}
